package com.carey.myblog.controller;

import com.carey.myblog.pojo.Type;
import com.carey.myblog.service.BlogService;
import com.carey.myblog.service.TypeService;
import com.carey.myblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TypeShowController {
    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @GetMapping("types/{id}")
    public String typeshow(@PathVariable Long id, Pageable pageable, Model model){
        List<Type> list = typeService.listTypeTop(100000);
        if (id == -1){
            id = list.get(0).getId();
        }
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        blogQuery.setPublished(true);
        model.addAttribute("types",list);
        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));
        model.addAttribute("activeTypeId", id);
        return "types";
    }
}
